package cn.wang.web.stat;

import cn.wang.entity.EchartBarVo;
import cn.wang.entity.EchartPieVo;
import cn.wang.utils.FastJsonUtil;
import cn.wang.utils.MailUtils;
import cn.wang.web.BaseAction;
import cn.wang.web.job.MailJob;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.awt.geom.QuadCurve2D;
import java.util.List;

/**
 * Created by john on 2017/11/14.
 */
public class StatChartAction extends BaseAction {

    private String legendsale ;
    private String seriessale;
    @Autowired
    private SessionFactory sessionFactory;

    public String factorysale(){

        return "factorysale";
    }

    public String getfactorysale(){
        //获取session
        Session session=sessionFactory.openSession();
        Query query=session.createSQLQuery("select CP.FACTORY_NAME,sum(CP.cnumber) from CONTRACT_PRODUCT_C cp GROUP BY cp.factory_name");
        //得到list数组
        List<Object[]>factorysale=query.list();
        EchartBarVo vo=  new EchartBarVo();
        //页面需要name数组
        String[] cNo=new String[factorysale.size()];
        //页面需要数量数组
        String[] factoryName=new String[factorysale.size()];
        for (int i=0; i<factorysale.size();i++){
            String o =  factorysale.get(i)[0].toString();
            factoryName[i]=o;
            String d =  factorysale.get(i)[1].toString();
            cNo[i]=d;
        }
        //存入目标对象里
        vo.setFactoryName(factoryName);
        vo.setFactoryProductNo(cNo);
        session.close();
        //转为json数据
        String json=FastJsonUtil.toJSONString(vo);
        //打印到jsp
        FastJsonUtil.write_json(ServletActionContext.getResponse(),json);
        return NONE;
    }


    public String productsale(){
        Session session=sessionFactory.openSession();
        Query query=session.createSQLQuery("select * FROM (select cp.product_no,sum(cnumber) cNo FROM EXPORT_product_c cp " +
                "GROUP BY cp.product_no) cp1 where ROWNUM<=20 ORDER BY  cp1.cNo desc");
        List<Object[]>objects=query.list();
        //页面需要String[] 货号
        String[] productno= new String[objects.size()];
        //页面需要String[] 货号 数量对象数组

        EchartPieVo[] productnos= new EchartPieVo[objects.size()];

        for (int i=0;i<objects.size();i++){
            productno[i]=objects.get(i)[0].toString();
            EchartPieVo vo=new EchartPieVo();
            vo.setName(objects.get(i)[0].toString());
            vo.setValue(objects.get(i)[1].toString());
            productnos[i]=vo;
        }
        legendsale = FastJsonUtil.toJSONString(productno);
        seriessale = FastJsonUtil.toJSONString(productnos);
        return "productsale";
    }


    public String onlineinfo(){

        return "onlineinfo";
    }
    public String getonlineinfo(){
        Session session=sessionFactory.openSession();
        Query query=session.createSQLQuery("select oi.a1,nvl(oi1.cNo,0) from online_info_t oi LEFT JOIN (select to_char(lp.login_time,'hh24') hours ," +
                "count(lp.login_time) cNo from LOGIN_LOG_P lp GROUP BY to_char(lp.login_time,'hh24')) " +
                "oi1 on oi.a1=oi1.hours ORDER BY oi.a1\n");
        List<Object[]>objects=query.list();
        EchartBarVo vo=  new EchartBarVo();
        //页面需要name数组
        String[] cNo=new String[objects.size()];
        //页面需要数量数组
        String[] factoryName=new String[objects.size()];
        for (int i=0; i<objects.size();i++){
            factoryName[i]=  objects.get(i)[0].toString();
            cNo[i]=  objects.get(i)[1].toString();
        }
        //存入目标对象里
        vo.setFactoryName(factoryName);
        vo.setFactoryProductNo(cNo);
        session.close();
        //转为json数据
        String json=FastJsonUtil.toJSONString(vo);
        //打印到jsp
        FastJsonUtil.write_json(ServletActionContext.getResponse(),json);
        return NONE;
    }


    public String getLegendsale() {
        return legendsale;
    }

    public String getSeriessale() {
        return seriessale;
    }
}
